﻿@using MvcAgenda.Resources;
@model MvcAgenda.Models.ListViewModel<MvcAgenda.Domain.Entities.location>
@{
    ViewBag.Title = "Locations";
}
<style>
.bootbox-body .tdWLinks
{
    display:none;
    }
</style>
<script>
    function gettoken() {
        var tokenTag = '@Html.AntiForgeryToken()'
        var token = $(tokenTag).val()
        return token
    }
    function errorHappens(){
        $('.bottom-right').notify({ message: { text: "Error:The location cannot be deleted."} }).show();
    }
    $(document).on("click", ".deleteLink", function (e) {
        var link = $(this).attr("href"); // "get" the intended link in a var
        var html = $(this).parents('.list-group-item');
        e.preventDefault();
        bootbox.confirm("Are you sure you want to delete?:<br/>" + html.prop('outerHTML'), function (result) {
            if (result) {
                var formData = { __RequestVerificationToken: gettoken() };
                $.ajax({
                    url: link,
                    type: "POST",
                    data: formData,
                    success: function (data) {
                        if (data) {
                            var description = html.find(".list-group-item-heading").text();
                            html.remove();
                            $('.bottom-right').notify({ message: { text: description + " deleted! "} }).show();
                        }
                        else {
                            errorHappens();
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        errorHappens();
                    }
                });
            }
        });
    });
</script>

<div class="row">
    <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
        <h2>Locations</h2>
    </div>
    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
        <p>
    @Html.ActionLink("Add a location", "Create")
</p>
    </div>
</div>
@if (Model.Items.Count() == 0)
{
        <p>
            You have not locations.
        </p>
}
else
{
    <ul class="list-group">
        @foreach (var item in Model.Items)
        {
            <li class="list-group-item">
                <div class="row">
                    <div class="col-lg-10 col-md-10 col-sm-10 col-xs-12">
                        <h4 class="list-group-item-heading">
                           @Html.DisplayFor(modelItem => item.description)</h4>
                    </div>
                    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 tdWLinks">
                        @Html.FontAwesomeActionLink("Details", "Details", new { id = item.id }, "fa fa-search")
                        |
                        @Html.FontAwesomeActionLink("Edit", "Edit", new { id = item.id }, "fa fa-pencil")
                        |
                        @Html.FontAwesomeActionLink("Delete", "Delete", new { id = item.id }, "fa fa-trash-o", new { @class = "deleteLink" })
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            @Html.DisplayFor(modelItem => item.fullAddress)
                               </div>
                </div>
            </li>
            @*@Html.DisplayFor(modelItem => item.timezone)*@


        }
    </ul>


<div class="pager">
    @Html.PageLinks(Model.PagingInfo, x => Url.Action("Index", new { page = x }))
</div>
}
